package application;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class TetrisMenuBar extends JMenuBar implements ActionListener{

	private Tetris theTetris;

	private Configuration oDialogConfiguration;


	public static final int CONFIG = 1;
	public static final int RULES = 2;
	public static final int ABOUT = 3;

	public TetrisMenuBar(Tetris pTetris){
		theTetris = pTetris;

		//JMenuBar oMenuBar = new JMenuBar();
		JMenu oMenu1 = new JMenu("Partie");
		JMenuItem oM1 = new JMenuItem("Nouvelle");
		JMenuItem oM2 = new JMenuItem("Pause/Continuer");
		JMenuItem oM3 = new JMenuItem("Menu accueil");
		JMenuItem oM4 = new JMenuItem("Meilleurs scores");
		JMenuItem oM5 = new JMenuItem("Quitter");
		JMenu oMenu2 = new JMenu("Paramètres");
		JMenuItem oM21 = new JMenuItem("Configurer");
		JMenu oMenu3 = new JMenu("Aide");
		JMenuItem oM31 = new JMenuItem("Règles du jeu");
		JMenuItem oM32 = new JMenuItem("A propos");

		oM1.addActionListener(this);
		oM2.addActionListener(this);
		oM3.addActionListener(this);
		oM4.addActionListener(this);
		oM5.addActionListener(this);
		oM21.addActionListener(this);
		oM31.addActionListener(this);
		oM32.addActionListener(this);

		oMenu1.add(oM3);
		oMenu1.add(oM1);
		oMenu1.add(oM2);
		//oMenu1.add(oM3);
		oMenu1.add(oM4);
		oMenu1.add(oM5);
		oMenu2.add(oM21);
		oMenu3.add(oM31);
		oMenu3.add(oM32);
		this.add(oMenu1);
		this.add(oMenu2);
		this.add(oMenu3);
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		//System.out.println("Je rentre en action");
		if (evt.getSource()instanceof JMenuItem){
			// gestion des événements liés aux menus
			String ChoixOption = evt.getActionCommand();
			if (ChoixOption.equals("Nouvelle")){
				theTetris.newGame();
			}
			else if (ChoixOption.equals("Pause/Continuer")) theTetris.togglePause();
			else if (ChoixOption.equals("Menu accueil")) theTetris.showHomeMenu();
			else if (ChoixOption.equals("Meilleurs scores")) theTetris.showScorePanel();
			else if (ChoixOption.equals("Quitter")) theTetris.closeFrame();
			else if (ChoixOption.equals("Configurer")) windowConfigurer(1);		
			else if (ChoixOption.equals("Règles du jeu")) windowConfigurer(2);
			else if (ChoixOption.equals("A propos")) windowConfigurer(3);
		}
	} 

	public void windowConfigurer(int x){
		boolean pause = theTetris.isGamePaused();
		if(!pause) 
			theTetris.togglePause();

		JDialog oDialog = null;


		if(x == TetrisMenuBar.CONFIG){
			oDialogConfiguration = new Configuration(theTetris);
			oDialogConfiguration.setVisible(true);
		}else
		{
			if(x == TetrisMenuBar.RULES){
				oDialog = new Regles();
			}else if(x == TetrisMenuBar.ABOUT){
				oDialog = new Apropos();
			}
			oDialog.setVisible(true);
		}


		theTetris.getFrame().requestFocus();

		if(!pause) 
			theTetris.togglePause();
	}

	public Configuration getDialogConfiguration() {
		return oDialogConfiguration;
	}

	public void setDialogConfiguration(Configuration pDialogConfiguration) {
		this.oDialogConfiguration = pDialogConfiguration;
	}

}
